package com.ckx.blog.controller;

import com.ckx.constants.SystemConstants;
import com.ckx.domain.ResponseResult;
import com.ckx.domain.entity.Article;
import com.ckx.domain.vo.ArticleDetailVo;
import com.ckx.domain.vo.HotArticleVo;
import com.ckx.domain.vo.PageVo;
import com.ckx.service.ArticleService;
import com.ckx.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;
    @Autowired
    private RedisCache redisCache;

    @GetMapping("/hotArticleList")
    public ResponseResult hotArticleList() {
        List<HotArticleVo> articles = articleService.hotArticleList();
        return new ResponseResult<>().ok(articles);
    }

    @GetMapping("/articleList")
    public ResponseResult articleList(Integer pageNum, Integer pageSize, Long categoryId) {
        PageVo pageVo = articleService.articleList(pageNum, pageSize, categoryId);
        return ResponseResult.okResult(pageVo);
    }

    @GetMapping("/{id}")
    public ResponseResult articleDetail(@PathVariable Long id){

        ArticleDetailVo articleDetailVo = articleService.getArticleDetail(id);
        return ResponseResult.okResult(articleDetailVo);
    }

    @PutMapping("/updateViewCount/{id}")
    public void updateViewCount(@PathVariable String id){
        redisCache.incrementCount(SystemConstants.ARTICLE_VIEWCOUNT, id, 1);
    }


}
